package org.xx.armory.swing.components;

import org.xx.armory.swing.UIController;
import org.xx.armory.swing.UIControllers;

import javax.swing.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * 用于创建对话框的面板。
 */
public abstract class DialogPane
        extends AbstractPane<JDialog>
        implements WindowListener {
    public static final int OK = 1;
    public static final int CANCEL = 0;

    public static final int YES = 1;
    public static final int NO = 0;
    public static final int YES_TO_ALL = 2;
    public static final int NO_TO_ALL = -1;

    public static final int ABORT = 1;
    public static final int RETRY = 2;
    public static final int IGNORE = 3;

    private boolean fixedSize;
    private int result;

    protected DialogPane() {
        this.fixedSize = true;
        this.result = CANCEL;

        this.controller().connect("ok", event -> done(OK));
        this.controller().connect("cancel", event -> done(CANCEL));
        this.controller().connect("yes", event -> done(YES));
        this.controller().connect("no", event -> done(NO));
        this.controller().connect("yes-to-all", event -> done(YES_TO_ALL));
        this.controller().connect("no-to-all", event -> done(NO_TO_ALL));
        this.controller().connect("abort", event -> done(ABORT));
        this.controller().connect("retry", event -> done(RETRY));
        this.controller().connect("ignore", event -> done(IGNORE));

        this.controller().connect("close", event -> done(CANCEL));
    }

    /**
     * 判断此对话框的尺寸是否不可变。
     *
     * @return 此对话框的尺寸是否不可变。
     */
    public final boolean isFixedSize() {
        return this.fixedSize;
    }

    /**
     * 设置此对话框的尺寸是否不可变。
     *
     * @param fixedSize
     *         此对话框的尺寸是否不可变。
     */
    public final void setFixedSize(
            boolean fixedSize
    ) {
        final boolean oldFixedSize = this.fixedSize;
        this.fixedSize = fixedSize;

        firePropertyChange("fixedSize", oldFixedSize, this.fixedSize);
    }

    /**
     * 获取对话框的执行结果。
     *
     * @return 对话框的执行结果。
     */
    public final int getResult() {
        return this.result;
    }

    /**
     * 调整对话框的尺寸和最小尺寸，都设置为最合适尺寸。
     */
    public final void pack() {
        final JDialog dialog = getOwner();
        if (dialog != null) {
            dialog.setMinimumSize(dialog.getPreferredSize());
            dialog.pack();
        }
    }

    /**
     * 表示对话框已执行完毕。
     *
     * @param result
     *         执行结果。
     */
    protected void done(
            int result
    ) {
        this.result = result;
        close();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected final UIController createUIController() {
        return UIControllers.createDialogUIController(this);
    }

    /**
     * 关闭对话框。
     */
    protected final void close() {
        if (this.getOwner() != null) {
            this.getOwner().setVisible(false);
        }
    }

    @Override
    public void windowOpened(
            WindowEvent event
    ) {
    }

    @Override
    public void windowClosing(
            WindowEvent event
    ) {
        event.getWindow().dispose();
    }

    @Override
    public void windowClosed(
            WindowEvent event
    ) {
    }

    @Override
    public void windowIconified(
            WindowEvent event
    ) {
    }

    @Override
    public void windowDeiconified(
            WindowEvent event
    ) {
    }

    @Override
    public void windowActivated(
            WindowEvent event
    ) {
    }

    @Override
    public void windowDeactivated(
            WindowEvent event
    ) {
    }
}
